const db = require('../db/echarts.js')

exports.save_currentEchartsList = (req,res)=>{
  const req_body = req.body
  console.log('req.body',req.body)
  //定义数据库查询语句
  const sqlStr = 'select * from ev_echart_config where _id = ?'

  db.query(sqlStr,req_body.id,(err,results)=>{
    if(err) return res.cc(err)

    if(results.length>0) return res.cc()

    const sqlInsert = 'insert into ev_echart_config set ?'
    const sqlInsertOpt = ' and insert into ev_echart_config_option set ?'
    db.query(sqlInsert,
      {
        // option_id:req_body.id,
        type:req_body.type,
        x:req_body.x,
        y:req_body.y,
        width:req_body.width,
        height:req_body.height},(err,results)=>{
          console.log("results",results)
          if(err) return res.cc(err)
          if(results.affectedRows !==1) return res.cc('保存失败')
          db.query(sqlInsertOpt,{
          
            backgroundColor:req_body.option.backgroundColor,
            titile_text:req_body.option.title.text,
            xAxis:JSON.stringify(req_body.option.xAxis.data),
            color:JSON.stringify(req_body.option.color),
            
            yAxis:JSON.stringify(req_body.option.yAxis.data),
            _id:results.insertId
          },(err,results)=>{
            if(err) return res.cc(err)
            if(results.affectedRows !==1) return res.cc('保存失败')
            res.cc("保存成功")
          })
          
        })
  })

  
}